package com.example.new6.views;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;


import com.example.new6.R;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import org.json.JSONObject;

import java.io.IOException;

public class TranslateActivity extends AppCompatActivity {
    private EditText sourceText;
    private TextView targetText;
    private Button translateButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translate);

        sourceText = findViewById(R.id.source_text);
        targetText = findViewById(R.id.target_text);
        translateButton = findViewById(R.id.translate_button);

        translateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String textToTranslate = sourceText.getText().toString();
                if (!textToTranslate.isEmpty()) {
                    translateText(textToTranslate);
                } else {
                    targetText.setText("Please enter text to translate.");
                }
            }
        });
    }

    private void translateText(String sourceText) {
        OkHttpClient client = new OkHttpClient();
        MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
        String json = "{\"sourceText\":\"" + sourceText + "\"}";
        RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);

        Request request = new Request.Builder()
                .url("https://api.oioweb.cn/api/txt/QQFanyi?sourceText=" + sourceText)
                .post(body)
                .build();

        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(() -> targetText.setText("Error: " + e.getMessage()));
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String jsonData = response.body().string();
                    runOnUiThread(() -> {
                        try {
                            JSONObject jsonObject = new JSONObject(jsonData);
                            if (jsonObject.getInt("code") == 200) {
                                JSONObject result = jsonObject.getJSONObject("result");
                                String target_Text = result.getString("targetText");
                                targetText.setText(target_Text);
                            } else {
                                targetText.setText("Error: " + jsonObject.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            targetText.setText("Error: " + e.getMessage());
                        }
                    });
                }
            }
        });
    }
}